package cn.nb12.xbox.utils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class GetGame {
	
	//游戏json数据链接
	private static String url="https://www.xbox.com/en-US/games/xbox-one/js/bi-urls.json";
	//游戏链接
	public static String gamesUrl="https://www.microsoft.com/<charset>/store/p/a/<code>";
	
	private static Logger log=LoggerFactory.getLogger(GetGame.class);
	
	/**
	 * 获取所有游戏链接
	 * @param areas 地区
	 * @return
	 * @throws Exception 
	 */
	public static List<String> getGames(String area) throws Exception{
		List<String> codes = getData();//所有游戏代码
		return getGames(codes,area);
	}
	
	/**
	 * 获取所有游戏链接
	 * @param areas 地区
	 * @return
	 * @throws Exception 
	 */
	public static List<String> getGames(List<String> codes,String area) throws Exception{
		return getGameUrls(codes,area);
	}
	
	/**
	 * 获取游戏json数据并解析出来
	 * @return
	 * @throws Exception
	 */
	public static List<String> getData() {
		String response;
		try {
			Map<String,String> headers=new HashMap<>();
			headers.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:57.0) Gecko/20100101 Firefox/57.0");
			headers.put("Content-Type", "application/json; charset=utf-8");
			response = RequestUtil.sendGet(url,headers,"UTF-8");
			
			if(response!=null && response.length()>0) {
				//获得所有游戏
				List<String> fullGames = subString(response, "fullGameArray", ";");
				//所有游戏的代码
				List<String> result = getResult(fullGames);
				return result;
			}
			else {
				log.error("获取游戏json没有结果");
			}
		} catch (IOException e) {
			throw new RuntimeException("获取游戏json异常",e);
		}
		return null;
	}
	
	/**
	 * 根据游戏代码获得游戏链接
	 * @param gameCodes
	 * @return
	 */
	public static List<String> getGameUrls(List<String> gameCodes,String charset){
		List<String> gameUrls=new ArrayList<>();//游戏链接
		if(gameCodes!=null && gameCodes.size()>0) {
			for (String string : gameCodes) {
				gameUrls.add(getGameUrls(string,charset));//替换编码和代码
			}
		}
		return gameUrls;
	}
	
	/**
	 * 根据游戏代码获得游戏链接
	 * @param gameCodes
	 * @return
	 */
	public static String getGameUrls(String gameCode,String charset){
		return gamesUrl.replaceAll("<charset>", charset).replaceAll("<code>", gameCode);//替换编码和代码
	}
	
	
	
	
	
	/**
	 * 截取字符串
	 * @param str 原字符串
	 * @param start 截取开头
	 * @param end 截取结尾
	 * @return
	 */
	public static List<String> subString(String str,String start,String end) {
		Pattern pattern = Pattern.compile(start+"[^;]*"+end);//正则表达式
		Matcher matcher = pattern.matcher(str);
		List<String> list=new ArrayList<>();//结果
		while(matcher.find()) {
			list.add(matcher.group());
		}
		return list;
	}
	
	/**
	 * 解析结果
	 * @param fullGames
	 * @return
	 */
	public static List<String> getResult(List<String> fullGames){
		List<String> fullGamesResult=new ArrayList<>();
		if(fullGames!=null && fullGames.size()>0) {
			for (String string : fullGames) {
				string=string.substring(string.indexOf("=")+3, string.length()-2);
				String[] split = string.split(",");
				for (String string2 : split) {
					string2=string2.trim();
					if(string2.length()>2) {
						string2=string2.substring(1, string2.length()-1);
						fullGamesResult.add(string2);
					}
				}
			}
		}
		return fullGamesResult;
	}
	
	public static void main(String[] args) throws Exception {
		List<String> areas=new ArrayList<>();
		areas.add("zh-HK");
		areas.add("zh-TW");
		areas.add("en-US");
		/*List<String> games = getGames(areas);
		for (String string : games) {
			GetInfo.getInfo(string);
		}*/
	}

}